<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\UploadFilePrinter;

class UploadFileResponse extends AllAICSResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<return>');
        $end = strpos($body,'</return>');

        $body = substr($body,$start + 8,intval($end - $start -8));

        if ($body) {
            libxml_disable_entity_loader(false);

            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['responseCode'] ?? '';
            $message = $body['responseMessage'] ?? '未知错误';
            $this->setBody($body);
            if ($errorCode !== 'E0000') {
                $this->setError($message);
                return $this;
            }
        }

        return $this;
    }
    public function printerInit(): Response
    {
        $uploadFilePrinter = new UploadFilePrinter();

        $this->printer = $uploadFilePrinter;

        return $this;
    }
}
